package thread;

/**
 *测试多线程并发安全问题
 *
 */
public class SyncDemo1 {
    private static boolean success1=false;
    private static boolean success2=false;

    public static void main(String[] args) {
        Bank bank=new Bank();
        int num=1;
        while (true) {
            Thread t1 = new Thread(() -> success1 = bank.getMoney(20000));
            Thread t2 = new Thread(() -> success2 = bank.getMoney(20000));
            t1.start();
            t2.start();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (success1 && success2) {
                System.out.println("全部取出，出现并发安全问题");
                System.out.println("共测试了"+num);
                break;
            } else {
                num++;
                bank.saveAccount(20000);
                success1=false;
                success2=false;
            }
        }
    }
}
